home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / dev / src / td01_src.lha / td_r0.1 / sl3 / source / rawa / rawa.c next >
Encoding:
C/C++ Source or Header  |  1999-06-19  |  10.5 KB  |  442 lines

  1. /*
  2. **      $VER: rawa.c 1.00 (15.05.1999)
  3. **
  4. **      Creation date : 15.05.1999
  5. **
  6. **      Description       :
  7. **         Standart 3d extension module for tdo.library.
  8. **         Loads and saves the mesh as RAW ASCII file.
  9. **
  10. **
  11. **      Written by Stephan Bielmann
  12. **
  13. */
  14.  
  15. /*************************** Includes *******************************/
  16.  
  17. /*
  18. ** Ansi C includes
  19. */
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24.  
  25. /*
  26. ** Amiga includes
  27. */
  28. #include <dos/dos.h>
  29. #include <dos/stdio.h>
  30.  
  31. #include <clib/dos_protos.h>
  32.  
  33. /*
  34. ** Project includes
  35. */
  36. #include "tdo_public.h"
  37. #include "compiler.h"
  38. #include "tdo.h"
  39.  
  40. /**************************** Defines *******************************/
  41.  
  42. /*********************** Type definitions ***************************/
  43.  
  44. /*************************** Variables ******************************/
  45.  
  46. /********************** Private functions ***************************/
  47.  
  48. /********************** Public functions ****************************/
  49.  
  50. /****** geoa.library/tdo3XSave ******************************************
  51. *   NAME    
  52. *     tdo3XSave -- Saves the mesh as a 3D file.
  53. *
  54. *   SYNOPSIS
  55. *    error = tdo3XSave( meshhandle,filename,screen)
  56. *                       D1         D2        A0
  57. *
  58. *    ULONG tdo3XSave
  59. *         ( ULONG,STRPTR,struct Screen " );
  60. *
  61. *   FUNCTION
  62. *    The mesh will be saved in a 3 dimensional representation in the
  63. *    filename, without existence checks.
  64. *    The screen parameter is optional and only used if non-NULL, to
  65. *    display a window with additional or special parameters needed to
  66. *    save the mesh, or messages.
  67. *   INPUTS
  68. *     meshhandle    - A valid handle of a mesh.
  69. *    filename      - Name of the file to create.
  70. *    screen        - Pointer to the work screen.
  71. *    
  72. *   RESULT
  73. *     error - RCNOERROR      if all went well.
  74. *            RCNOTIMPL      if the function is not implemented.
  75. *            RCNOMEMORY     if there is not enough memory. 
  76. *            RCWRITEDATA    if an error occured while writing data, no more space.
  77. *            RCOVERFLOW     if the mesh is to extensive for this format.
  78. *            RCNOMATERIAL   if there are no materials, but they are expected.
  79. *           RCNOPART       if there are no parts, but they are expected.
  80. *            RCNOPOLYGON    if there are no polygons, but they are expected.
  81. *            RCNOVERTEX     if there are no vertices, but they are expected.
  82. *            IoErr()        if possible you will get this.
  83. *            tdo errors     if a tdo call failed.
  84. *   EXAMPLE
  85. *    error = tdo3XSave(meshhandle,"ram:test",NULL);
  86. *
  87. *   NOTES
  88. *    By setting the meshhandle to 0 the function returns RCNOERROR if
  89. *    it is implemented or RCNOTIMPL if not !
  90. *
  91. *   BUGS
  92. *   SEE ALSO
  93. ******************************************************************************
  94. *
  95. */
  96. ULONG __saveds ASM tdo3XSave(register __d1 ULONG meshhandle,
  97.                             register __d2 STRPTR filename,
  98.                             register __a0 struct Screen *screen) {
  99.  
  100.     BPTR            rawfile=NULL;
  101.     ULONG            mesh,nofv,nofpo,nofpa,i,j,n;
  102.     TTDOVertexf    v1,v2,v3;
  103.     UBYTE            buffer[100];
  104.  
  105.  
  106.     mesh=meshhandle;
  107.  
  108.     // we were asked for implementation
  109.     if(mesh==0) {
  110.         return(RCNOERROR);
  111.     }
  112.  
  113.     // Ensure that no current polygon is set
  114.     meshPartPolygonEnd(mesh);
  115.  
  116.     nofv=meshNofVerticesGet(mesh);
  117.     nofpo=meshNofPolygonsGet(mesh);
  118.     nofpa=meshNofPartsGet(mesh);
  119.  
  120.     // Check if there are some vertices
  121.     if(nofv==0) {
  122.         return(RCNOVERTEX);
  123.     }
  124.  
  125.     // Check if there are some polygons
  126.     if(nofpo==0) {
  127.         return(RCNOPOLYGON);
  128.     }
  129.  
  130.     // Check if there are some parts
  131.     if(nofpa==0) {
  132.         return(RCNOPART);
  133.     }
  134.  
  135.     /* Open the file for output */
  136.     if((rawfile=Open(filename,MODE_NEWFILE))==NULL) return(IoErr());
  137.  
  138.     /* Change the buffer size of the filehandle to 10k */
  139.     if (SetVBuf(rawfile,NULL,BUF_FULL,10000)!=DOSFALSE) {
  140.         Close(rawfile);
  141.         return(RCNOMEMORY);
  142.     }
  143.  
  144.     // Write the header
  145.     if (FPrintf(rawfile,"RAWA\n")==ENDSTREAMCH) {
  146.         Close(rawfile);
  147.         return(RCWRITEDATA);
  148.     }
  149.  
  150.     // Get all parts and write down theyr polygons with material
  151.     for(i=1;i<=nofpa;i++) {
  152.         if((nofpo=meshPartNofPolygonsGet(mesh,i))>0) {
  153.             for(j=1;j<=nofpo;j++) {
  154.                 meshPartPolygonCurrentSet(mesh,i,j);
  155.                 // we are saving triangles, so at least 3 vertices must be there
  156.  
  157.                 if((nofv=meshNofVerticesGet(mesh))>2) {
  158.                     meshVertexGetfv(mesh,1,&v1);
  159.  
  160.                     n=2;
  161.                     while(n<nofv) {
  162.                         meshVertexGetfv(mesh,n++,&v2);
  163.                         meshVertexGetfv(mesh,n,&v3);
  164.  
  165.                         sprintf(buffer,"%0.5g %0.5g %0.5g %0.5g %0.5g %0.5g %0.5g %0.5g %0.5g\n",v1.x,v1.y,v1.z,
  166.                         v2.x,v2.y,v2.z,v3.x,v3.y,v3.z);
  167.                         if(FPuts(rawfile,buffer)!=DOSFALSE) {
  168.                             Close(rawfile);
  169.                             return(RCWRITEDATA);
  170.                         }
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.     }
  176.  
  177.     // Close the file
  178.     Close(rawfile);
  179.  
  180.     return(RCNOERROR);
  181. }
  182.  
  183. /****** geoa.library/tdo3XLoad ******************************************
  184. *   NAME    
  185. *     tdo3XLoad -- Load a 3D file and creates a mesh.
  186. *
  187. *   SYNOPSIS
  188. *    error = tdo3XLoad( meshhandle,filename,erroffset,screen)
  189. *                       D1         D2       D3         A0
  190. *
  191. *    ULONG tdo3XLoad
  192. *         ( ULONG,STRPTR,ULONG *,struct Screen " );
  193. *
  194. *   FUNCTION
  195. *    A file which contains a 3 dimensional representation
  196. *    will be examined for all known elements and converted into the
  197. *    mesh.
  198. *    The screen parameter is optional and only used if non-NULL, to
  199. *    display a window with additional or special parameters needed to
  200. *    save the mesh or messages.
  201. *    If an error occurs, erroffset contains the file offset in lines for
  202. *    ascii files and bytes for binary formats.
  203. *   INPUTS
  204. *     meshhandle    - A valid handle of a mesh.
  205. *    filename      - Name of the file to load.
  206. *    erroffset     - Offset where a read error occured.
  207. *    screen        - Pointer to the work screen.
  208. *    
  209. *   RESULT
  210. *     error - RCNOERROR       if all went well.
  211. *            RCNOTIMPL       if the function is not implemented.
  212. *            RCNOMEMORY      if there is not enough memory. 
  213. *            RCUNKNOWNFORMAT if the file format is unknown.
  214. *            RCNOFILE        if the file is not found.
  215. *            RCREADDATA      if an error occured while reading data.
  216. *            RCNOMATERIAL    if there are no materials, but they are expected.
  217. *           RCNOPART        if there are no parts, but they are expected.
  218. *            RCNOPOLYGON     if there are no polygons, but they are expected.
  219. *            RCNOVERTEX      if there are no vertices, but they are expected.
  220. *            IoErr()         if possible you will get this.
  221. *            tdo errors      if a tdo call failed.
  222. *   EXAMPLE
  223. *    error = tdo3XLoad(meshhandle,"ram:test",NULL);
  224. *
  225. *   NOTES
  226. *    By setting the meshhandle to 0 the function returns RCNOERROR if
  227. *    it is implemented or RCNOTIMPL if not !
  228. *
  229. *   BUGS
  230. *   SEE ALSO
  231. ******************************************************************************
  232. *
  233. */
  234. ULONG __saveds ASM tdo3XLoad(register __d1 ULONG meshhandle,
  235.                             register __d2 STRPTR filename,
  236.                             register __d3 ULONG *erroffset,
  237.                             register __a0 struct Screen *screen) {
  238.  
  239.     ULONG mesh;
  240.     FILE *fp=NULL;
  241.     char header[10];
  242.     TTDOFloat af[9];
  243.     TTDOColorub color;
  244.  
  245.  
  246.     mesh=meshhandle;
  247.  
  248.     // we were asked for implementation
  249.     if(mesh==0) {
  250.         return(RCNOERROR);
  251.     }
  252.  
  253.     // we have one white material and one part
  254.     meshMaterialAdd(mesh);
  255.     color.r=255,color.g=255,color.b=255;
  256.     meshMaterialDiffuseColorSetubc(mesh,1,&color);
  257.     meshPartAdd(mesh);
  258.     meshPartMaterialSet(mesh,1,1);
  259.     
  260.     // check if all went well
  261.     if(meshNofMaterialsGet(mesh)!=1 || meshNofPartsGet(mesh)!=1) {
  262.         return(RCNOMEMORY);
  263.     }
  264.  
  265.     fp=fopen(filename,"r");
  266.     if(fp==NULL) {
  267.         return(RCNOFILE);
  268.     }
  269.  
  270.     // initializing the line counter
  271.     (*erroffset)=1;
  272.  
  273.     fscanf(fp,"%4s",header);
  274.     if(strncmp(header,"RAWA",4)!=0) {
  275.         fclose(fp);
  276.         return(RCUNKNOWNFORMAT);
  277.     }
  278.         
  279.     (*erroffset)++;
  280.  
  281.     // ensure no current polygon
  282.     meshPartPolygonEnd(mesh);
  283.  
  284.     while(fscanf(fp,"%f %f %f %f %f %f %f %f %f",&af[0],&af[1],&af[2],&af[3],&af[4],&af[5],&af[6],&af[7],&af[8])!=EOF) {
  285.         meshPartTriangleAdd9fa(mesh,1,af);
  286.         (*erroffset)++;
  287.     }
  288.     
  289.     return(RCNOERROR);
  290. }
  291.  
  292. /****** geoa.library/tdo3XCheckFile ******************************************
  293. *   NAME    
  294. *     tdo3XCheckFile -- Checks if the file is in the format we expected to load.
  295. *
  296. *   SYNOPSIS
  297. *    error = tdo3XLoad( filename )
  298. *                       D1
  299. *
  300. *    ULONG tdo3XCheckFile
  301. *         ( STRPTR );
  302. *
  303. *   FUNCTION
  304. *    The file its header will be examinated to verify if it is the
  305. *    file format we expect to read with the load function.
  306. *   INPUTS
  307. *    filename      - Name of the file to load.
  308. *    
  309. *   RESULT
  310. *     error - RCNOERROR       if all went well.
  311. *            RCUNKNOWNFORMAT if the file format is unknown.
  312. *            RCNOFILE        if the file is not found.
  313. *            RCREADDATA      if an error occured while reading data.
  314. *            IoErr()         if possible you will get this.
  315. *   EXAMPLE
  316. *    error = tdo3XCheckFile("ram:test");
  317. *
  318. *   NOTES
  319. *
  320. *   BUGS
  321. *   SEE ALSO
  322. ******************************************************************************
  323. *
  324. */
  325. ULONG __saveds ASM tdo3XCheckFile(register __d2 STRPTR filename) {
  326.  
  327.     FILE *fp=NULL;
  328.     char header[10];
  329.  
  330.     // open the file for input
  331.     fp=fopen(filename,"r");
  332.     if(fp==NULL) {
  333.         return(RCNOFILE);
  334.     }
  335.  
  336.     // check the file type
  337.     fscanf(fp,"%4s",header);
  338.     fclose(fp);
  339.  
  340.     if(strncmp(header,"RAWA",4)!=0) {
  341.         return(RCUNKNOWNFORMAT);
  342.     }
  343.  
  344.     return(RCNOERROR);
  345. }
  346.  
  347. /****** geoa.library/tdo3XExt ******************************************
  348. *   NAME    
  349. *     tdo3XExt -- Returns the default extension of the file format.
  350. *
  351. *   SYNOPSIS
  352. *    ext = tdo3XExt ( )
  353. *
  354. *    SRPTR tdo3XExt
  355. *         ( );
  356. *
  357. *   FUNCTION
  358. *    The default extension of the file format will be returned
  359. *    as READ_ONLY, NULL terminated string which will be only valid
  360. *    as long as the library is opened.
  361. *   INPUTS
  362. *    
  363. *   RESULT
  364. *     ext - String pointer to the extension, or NULL no default.
  365. *   EXAMPLE
  366. *    ext = tdo3XExt();
  367. *
  368. *   NOTES
  369. *
  370. *   BUGS
  371. *   SEE ALSO
  372. ******************************************************************************
  373. *
  374. */
  375. STRPTR __saveds ASM tdo3XExt () {
  376.     static STRPTR ext="raw";
  377.  
  378.     return(ext);
  379. }
  380.  
  381. /****** geoa.library/tdo3XName ******************************************
  382. *   NAME    
  383. *     tdo3XName -- Returns the file format name string.
  384. *
  385. *   SYNOPSIS
  386. *    name = tdo3XName ( )
  387. *
  388. *    SRPTR tdo3XName
  389. *         ( );
  390. *
  391. *   FUNCTION
  392. *    The file format its name will be returned. This string should not 
  393. *    be to large, about 20 characters maximum.
  394. *    The string is READ_ONLY, NULL terminated and only valid as
  395. *    long as the library is opened.
  396. *   INPUTS
  397. *    
  398. *   RESULT
  399. *     name - String pointer to the name, or NULL no one.
  400. *   EXAMPLE
  401. *    name = tdo3XName();
  402. *
  403. *   NOTES
  404. *
  405. *   BUGS
  406. *   SEE ALSO
  407. ******************************************************************************
  408. *
  409. */
  410. STRPTR __saveds ASM tdo3XName () {
  411.     static STRPTR name="RAW ASCII (TD)";
  412.  
  413.     return(name);
  414. }
  415.  
  416. /************************* End of file ******************************/
  417.